var pomelo = require('pomelo');
var utils = require('../../util/utils');
var logger = require('pomelo-logger').getLogger('KillerGame', __filename);
var Consts = require('../../util/consts');
var KillerUser = require('../../domain/killer/killerUser');

var killerUserDao = module.exports;

killerUserDao.createUser = function (username, rid, isConfirm, isLive, role, status, seq, cb) {
    var sql = "insert into killer_user (name, rid, isConfirm, isLive, role, status, seq) values (?, ?, ?, ?, ?, ?, ?)";
    var args = [username, rid, isConfirm, isLive, role, status, seq];

    pomelo.app.get('dbclient').query(sql, args, function (err, res) {
        if (!!err) {
            utils.invokeCallback(cb, {code: err.number, msg: err.message}, null);
        } else {
            var user = res;
            utils.invokeCallback(cb, null, user);
        }
    });
};

killerUserDao.getAliveUsersByRidAndRole = function (rid, role, cb) {
    var sql = "select * from killer_user where rid = ? and role = ? and isLive = true";
    var args = [rid, role];
    pomelo.app.get('dbclient').query(sql, args, function (err, res) {
            if (!!err) {
                utils.invokeCallback(cb, {code: err.number, msg: err.messsage}, null);
            } else {
                var userList = [];
                if (res.length > 0) {
                    for (var i = 0; i < res.length; i++) {
                        var dbUser = res[i];
                        userList.push(new KillerUser({
                            id: dbUser.id,
                            name: dbUser.name, rid: dbUser.rid,
                            isConfirm: dbUser.isConfirm, isLive: dbUser.isLive,
                            role: dbUser.role, seq: dbUser.seq
                        }));
                    }
                    utils.invokeCallback(cb, null, userList);
                } else {
                    utils.invokeCallback(cb, null, []);
                }
            }
        }
    );
};

killerUserDao.getUsersByRidAndRole = function (rid, role, cb) {
    var sql = "select * from killer_user where rid = ? and role = ?";
    var args = [rid, role];
    pomelo.app.get('dbclient').query(sql, args, function (err, res) {
            if (!!err) {
                utils.invokeCallback(cb, {code: err.number, msg: err.messsage}, null);
            } else {
                var userList = [];
                if (res.length > 0) {
                    for (var i = 0; i < res.length; i++) {
                        var dbUser = res[i];
                        userList.push(new KillerUser({
                            id: dbUser.id,
                            name: dbUser.name, rid: dbUser.rid,
                            isConfirm: dbUser.isConfirm, isLive: dbUser.isLive,
                            role: dbUser.role, seq: dbUser.seq
                        }));
                    }
                    utils.invokeCallback(cb, null, userList);
                } else {
                    utils.invokeCallback(cb, null, []);
                }
            }
        }
    );
};


killerUserDao.getUsersByRid = function (rid, cb) {
    var sql = "select * from killer_user where rid = ?";
    var args = [rid];
    pomelo.app.get('dbclient').query(sql, args, function (err, res) {
            if (!!err) {
                utils.invokeCallback(cb, {code: err.number, msg: err.message}, null);
            } else {
                var userList = [];
                if (res.length > 0) {
                    for (var i = 0; i < res.length; i++) {
                        var dbUser = res[i];
                        userList.push(new KillerUser({
                            id: dbUser.id,
                            name: dbUser.name, rid: dbUser.rid,
                            isConfirm: dbUser.isConfirm, isLive: dbUser.isLive,
                            role: dbUser.role, seq: dbUser.seq
                        }));
                    }
                    utils.invokeCallback(cb, null, userList);
                } else {
                    utils.invokeCallback(cb, null, []);
                }
            }
        }
    );
};

killerUserDao.getAliveUsersByRid = function (rid, cb) {
    var sql = "select * from killer_user where rid = ? and isLive = true and isConfirm = true";
    var args = [rid];
    pomelo.app.get('dbclient').query(sql, args, function (err, res) {
            if (!!err) {
                utils.invokeCallback(cb, {code: err.number, msg: err.message}, null);
            } else {
                var userList = [];
                if (res.length > 0) {
                    for (var i = 0; i < res.length; i++) {
                        var dbUser = res[i];
                        userList.push(new KillerUser({
                            id: dbUser.id,
                            name: dbUser.name, rid: dbUser.rid,
                            isConfirm: dbUser.isConfirm, isLive: dbUser.isLive,
                            role: dbUser.role, seq: dbUser.seq
                        }));
                    }
                    utils.invokeCallback(cb, null, userList);
                } else {
                    utils.invokeCallback(cb, null, []);
                }
            }
        }
    );
};

killerUserDao.getUserByID = function (username, rid, cb) {
    var sql = "select * from killer_user where rid = ? and name = ?";
    var args = [rid, username];
    pomelo.app.get('dbclient').query(sql, args, function (err, res) {
        if (!!err) {
            utils.invokeCallback(cb, {code: err.number, msg: err.message}, null);
        } else {
            if (res.length > 0) {
                var killerUser = res[0];
                utils.invokeCallback(cb, null, killerUser);
            } else {
                utils.invokeCallback(cb, null, {});
            }
        }
    });
};

killerUserDao.getNoConfirmUsers = function (rid, cb) {
    var sql = "select * from killer_user where rid = ? and isLive = true and isConfirm = false and status = ?";
    var args = [rid, Consts.KillerPhase.INIT];
    pomelo.app.get('dbclient').query(sql, args, function (err, res) {
            if (!!err) {
                utils.invokeCallback(cb, {code: err.number, msg: err.message}, null);
            } else {
                var userList = [];
                if (res.length > 0) {
                    for (var i = 0; i < res.length; i++) {
                        var dbUser = res[i];
                        userList.push(new KillerUser({
                            id: dbUser.id,
                            name: dbUser.name, rid: dbUser.rid,
                            isConfirm: dbUser.isConfirm, isLive: dbUser.isLive,
                            role: dbUser.role, seq: dbUser.seq
                        }));
                    }
                    utils.invokeCallback(cb, null, userList);
                } else {
                    utils.invokeCallback(cb, null, []);
                }
            }
        }
    );
};

killerUserDao.updateUser = function (rid, username, isConfirm, isLive, status, cb) {
    var sql = "update killer_user set isConfirm = ? , isLive = ? , status = ?  where rid = ? and name = ?";
    var args = [isConfirm, isLive, status, rid, username];
    pomelo.app.get('dbclient').query(sql, args, function (err, res) {
        if (!!err) {
            utils.invokeCallback(cb, {code: err.number, msg: err.message}, null);
        } else {
            utils.invokeCallback(cb, null, res);
        }
    });

};

killerUserDao.killUser = function (rid, username, cb) {
    var sql = "update killer_user set isLive = false  where rid = ? and name = ?";
    var args = [rid, username];
    pomelo.app.get('dbclient').query(sql, args, function (err, res) {
        if (!!err) {
            utils.invokeCallback(cb, {code: err.number, msg: err.message}, null);
        } else {
            utils.invokeCallback(cb, null, res);
        }
    });
};

killerUserDao.updateUserStatus = function (rid, username, status, cb) {
    var sql = "update killer_user set status = ? where rid = ? and username = ?";
    var args = [status, rid, username];
    pomelo.app.get('dbclient').query(sql, args, function (err, res) {
        if (err) {
            logger.error("update user status failed: %j", err);
        } else {
            utils.invokeCallback(cb, null, res);
        }
    });
};